package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.BannerComponent;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.BannerInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.BannerService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.BannerVo;
import com.google.gson.Gson;

@Component("bannerComponent")
public class BannerComponentImpl implements BannerComponent {

	private static final Logger log = Logger
			.getLogger(BannerComponentImpl.class);
	private Gson gson;
	@Resource(name = "bannerService")
	private BannerService bannerService;

	public BannerComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<BannerInfo> queryList(String body, Head head) {
		BannerVo vo = gson.fromJson(body, BannerVo.class);
		if (vo != null) {
			String belong = vo.getBannerBelong();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("bannerBelong", belong);
			List<BannerInfo> list = bannerService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (BannerInfo activ : list) {
					if (StringUtils.isNotBlank(activ.getBannerPath())) {
						activ.setBannerPath(CommonUtil.getUrlPrefix()
								+ activ.getBannerPath());
					}
				}
				return list;
			} else {
				return new ArrayList<BannerInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取banner信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
